/**
 * @file Button.cpp
 * @see http://code.google.com/p/mega8-hob/source/browse/trunk/Button.cpp
 * @author Hamish Cumming & Brian Thorne
 * @version see SVN revision number
 */
#include <stdlib.h>
#include "button.h"
#include "atto.h"

/**
 * Returns a button structure with the given position and size
 * and state OUT.
 *
 * Could use atto_measure_string(const char *string, int *w, int *h); to make a button and not give it the size
 * Just the position and label...
 * @param top The y coordinate of the button to be made.
 * @param left The x coordinate of the button to be made.
 * @param h The height of the button.
 * @param w The width of the button.
 * @param action A pointer to a function with no inputs that returns void which gets called on the button being pressed.
 * @param c_in The Colour of the button when depressed/selected.
 * @param c_out The colour of the button usually.
 * @param c_edge_in Colour of the edge of the button when deppressed.
 * @param c_edge_out Colour of the edge of the button.
 * @return The button created of type button_t
 * @see button.h
 */
button_t make_button (int top, int left, int h, int w, char * label,void (*action)(void),colour_t c_in, colour_t c_out,colour_t c_edge_in, colour_t c_edge_out)
{
   button_t button;
   button.top = top;
   button.left = left;
   button.h = h;
   button.w = w;
   button.state = BUT_OUT;
   button.label = label;
   button.action = action;
   button.colour_in = c_in;
   button.colour_out = c_out;
   button.colour_edge_in = c_edge_in;
   button.colour_edge_out = c_edge_out;
   return button;
}

int mouse_in_button (int mouse_x, int mouse_y, button_t * button)
{
   Assert (button != NULL);  /* Button should be initialised on startup */
   return (mouse_x >= button->left && mouse_x <= button->left + button->w && mouse_y >= button->top  && mouse_y <= button->top  + button->h);
}

void change_button_state (button_t *button)
{
   Assert (button != NULL);  /* Buttons should be initialised on startup */
   if (button->state == BUT_OUT)
      button->state = BUT_IN;
   else
      button->state = BUT_OUT;
}

int get_button_state (button_t *button)
{
   Assert (button != NULL);  /* Buttons should be initialised on startup */
   return button->state;
}

void draw_button (button_t * button)
{
   Assert (button != NULL);  /* Button should be initialised on startup */
   /* Draw edge */
    if (button->state == BUT_INVIS)
        return;
    
   
    switch (button->state)
    {
    case BUT_OUT:
         atto_color (button->colour_edge_out.r, button->colour_edge_out.g, button->colour_edge_out.b);
         atto_fill (button->left - 2, button->top - 2, button->w + 4, button->h + 4); 
         atto_color (button->colour_out.r, button->colour_out.g, button->colour_out.b);
         break;
    case BUT_IN:
         atto_color (button->colour_edge_in.r, button->colour_edge_in.g, button->colour_edge_in.b);
         atto_fill (button->left - 2, button->top - 2, button->w + 4, button->h + 4); 
         atto_color (button->colour_in.r, button->colour_in.g, button->colour_in.b);
         break;
   }
   atto_fill (button->left, button->top, button->w, button->h);
   atto_color(black.r,black.g,black.b);
   atto_string (button->left+5 , button->top+20, button->label);
}

